﻿using Microsoft.AspNet.OData.Extensions;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;

namespace aier.wx.api.selfhost
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
               name: "ActionApi",
               routeTemplate: "api/{controller}/{action}/{id}",
               defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );


            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            jsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            jsonFormatter.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

            config.EnableDependencyInjection();
            config.EnsureInitialized();
            config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");

            var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);
            
        }
    }
}
